package org.example.cloud.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

@SpringBootApplication
//@EnableEurekaClient
@EnableDiscoveryClient //启用服务发现（支持zk,consul注册中心）
@EnableScheduling
@EnableFeignClients
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @Scheduled(cron = "10 * * * * ?")
    public void getDiscoveryClientInfo(){
        System.out.println("服务发现------");
        List<String> services = discoveryClient.getServices();

        for(String ele:services){
            System.out.println("发现服务："+ele);

            List<ServiceInstance> instances = discoveryClient.getInstances(ele);

            for (ServiceInstance serviceInstance:instances){
                System.out.println("\t服务"+ele+serviceInstance.getInstanceId()+"->"+serviceInstance.getServiceId()+"->"+serviceInstance.getUri());
            }
            System.out.println("发现服务："+ele);
        }

        System.out.println("服务发现------");
    }

}
